<?php


namespace app\admin\controller\pointgoods;


use app\common\controller\Adminbase;
use think\Db;

class Pointgoods extends Adminbase
{
    protected $searchFields = 'name';
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $_list = Db::name('points_goods')
                ->order(array('create_time'=>'desc','id' => 'desc'))
                ->where($where)
                ->where('is_del', '<>', 1)
                ->page($page, $limit)
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }
            $total = Db::name('points_goods')
                ->where($where)
                ->where('is_del', '<>', 1)
                ->count();
            $result = array("code" => 0, "count" => $total, "data" => $_list);

            return json($result);
        }

        return $this->fetch();
    }

    //增加
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $insert_data = array(
                'points' => $data['points'],
                'name' => $data['name'],
                'img' => $data['img'],
                'sort'=>$data['sort'],
                'sale_num'=>$data['sale_num'],
                'content'=>$data['content'],
                'create_time'=>time(),
            );
            $stmt = Db::name('points_goods')->insertGetId($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        return $this->fetch();
    }

    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('points_goods')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }



    //删除
    public function del()
    {

        $ids = $this->request->param('ids/a', null);

        try {
            foreach ($ids as $id) {
                Db::name('points_goods')->where('id', $id)->update([
                    'is_del' => 1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $insert_data = array(
                'id' => $data['id'],
                'points' => $data['points'],
                'name' => $data['name'],
                'img' => $data['img'],
                'sort'=>$data['sort'],
                'sale_num'=>$data['sale_num'],
                'content'=>$data['content'],
                'create_time'=>time(),
            );
            $stmt = Db::name('points_goods')->update($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        } else {
            $id = $this->request->param('id/d', 0);
            $data = Db::name('points_goods')->find($id);
            $this->assign(['data'=>$data]);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }
}